#include "mainwindow.h"
#include "ui_mainwindow.h"



MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{

    ui->setupUi(this);
    //初始化发送数据
    controll_data[1]=0;
    controll_data[2]=0;
    controll_data[3]=0;
    ui->LED->setStyleSheet("background-color: rgb(255, 0, 0);");//设置为红色
    ui->Buzzer->setStyleSheet("background-color: rgb(255, 0, 0);");//设置为红色

    qmlObject=ui->quickWidgetVedio->rootObject();//连接qml

    //启动发送定时器
    sendTimer= new QTimer(this);
    sendTimer->start(50);
    connect(sendTimer, SIGNAL(timeout()), this, SLOT(send_data()));
    //初始化TCP客户端
    tcpClient= new QTcpSocket(this);
    tcpClient->abort();
    connect(tcpClient,SIGNAL(readyRead()),this,SLOT(receive_data()));
    connect(tcpClient,SIGNAL(disconnected()),this,SLOT(tcp_disconnect()));
//    connect(tcpClient,SIGNAL(AbstractSocket::SocketError),this,SLOT(tcp_disconnect()));


}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::send_data()
{

    controll_data[0]=0x5A;
    controll_data[7]=0xA5;

    if(tcpClient->state()==QAbstractSocket::ConnectedState)
    {
        char buff1=qmlObject->property("leftPercent").toInt();
        char buff2=qmlObject->property("rightPercent").toInt();

        if((buff1<=10)||(buff1>=246))buff1=0;//限定油门范围
        if((buff2<=10)||(buff2>=246))buff2=0;
        controll_data[1]=buff1;
        controll_data[2]=buff2;
        //    qDebug()<<controll_data[1]<<":"<<controll_data[2];
        tcpClient->write((const char*)controll_data,8);
    }
    ui->recStatus->setStyleSheet("background-color: rgb(255,0,0);");//设置为红色
}
void MainWindow::receive_data()
{
    static QByteArray buffer;

    if(buffer.size()<8)
    {
        buffer.append(tcpClient->readAll());
    }
    else
    {
        if( ((unsigned char)(buffer.data()[0])==0x5A) && \
            ((unsigned char)(buffer.data()[7])==0xA5))
        {
            ui->recStatus->setStyleSheet("background-color: rgb(0, 255, 0);");//设置为绿色
        }

    }
}
void MainWindow::on_Connect_clicked()
{
    ui->Connect->setStyleSheet("color: rgb(0, 0, 0);");//设置为黑色
    if(tcpClient->state()==0)//如果没有连接
    {
        ui->Connect->setText("连接");

        qmlObject->setProperty("leftPercent",0);
        qmlObject->setProperty("rightPercent",0);
        qmlObject->setProperty("leftPointy",qmlObject->property("midPointy"));
        qmlObject->setProperty("rightPointy",qmlObject->property("midPointy"));
        qmlObject->setProperty("leftWheelBlockColor","red");
        qmlObject->setProperty("rightWheelBlockColor","red");

        controll_data[1]=0;
        controll_data[2]=0;
        QObject *rect = qmlObject->findChild<QObject*>("vedio");
        if(rect){
            rect->property("reload:");
        }

        tcpClient->connectToHost("192.168.4.1",1314);
        if(tcpClient->waitForConnected(1000))//如果连接成功进入if
        {
            ui->Connect->setText("已连接");
        }
    }
    else
    {
        tcpClient->disconnectFromHost();
        ui->Connect->setText("连接");


        qmlObject->setProperty("leftPercent",0);
        qmlObject->setProperty("rightPercent",0);
        qmlObject->setProperty("leftPointy",qmlObject->property("midPointy"));
        qmlObject->setProperty("rightPointy",qmlObject->property("midPointy"));
        qmlObject->setProperty("leftWheelBlockColor","red");
        qmlObject->setProperty("rightWheelBlockColor","red");

        controll_data[1]=0;
        controll_data[2]=0;

//        vedio->stop();
    }
}
void MainWindow::tcp_disconnect()
{
    ui->Connect->setText("连接");
    ui->Connect->setStyleSheet("color: rgb(255, 0, 0);");//设置为红色


    qmlObject->setProperty("leftPercent",0);
    qmlObject->setProperty("rightPercent",0);
    qmlObject->setProperty("leftPointy",qmlObject->property("midPointy"));
    qmlObject->setProperty("rightPointy",qmlObject->property("midPointy"));
    qmlObject->setProperty("leftWheelBlockColor","red");
    qmlObject->setProperty("rightWheelBlockColor","red");

    controll_data[1]=0;
    controll_data[2]=0;
}

void MainWindow::on_LED_clicked()
{
    if(ui->LED->text()=="灯已关")
    {
        controll_data[3]|=0x02;
        ui->LED->setText("灯已开");
        ui->LED->setStyleSheet("background-color: rgb(0, 255, 0);");//设置为绿色
    }
    else
    {
        controll_data[3]&=(~0x02);
        ui->LED->setText("灯已关");
        ui->LED->setStyleSheet("background-color: rgb(255, 0, 0);");//设置为红色
    }
}

void MainWindow::on_Buzzer_pressed()
{
    controll_data[3]|=0x01;
    ui->Buzzer->setStyleSheet("background-color: rgb(0, 255, 0);");//设置为绿色
}

void MainWindow::on_Buzzer_released()
{
    controll_data[3]&=(~0x01);
    ui->Buzzer->setStyleSheet("background-color: rgb(255, 0, 0);");//设置为红色
}


